package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompMchntManageService;
import cc.rengu.igas.bomp.facade.request.*;
import cc.rengu.igas.bomp.facade.response.*;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class BompMchntManageServiceImpl implements BompMchntManageService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MchntBaseInfoManageResponse> mchntBaseInfoManage(MchntBaseInfoManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBaseInfoManageResponse> result = new Result<>();
        MchntBaseInfoManageResponse mchntBaseInfoManageResponse = new MchntBaseInfoManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntBaseInfoManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntBaseInfoManageResponse = (MchntBaseInfoManageResponse) dubboResult.getResult();
            result.setResult(mchntBaseInfoManageResponse);
            mchntBaseInfoManageResponse.setRespCode(dubboResult.getRespCode());
            mchntBaseInfoManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntBaseInfoManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户基本信息管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntBaseInfoManageResponse.setRespCode(((BizException) e).getCode());
                mchntBaseInfoManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntBaseInfoManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntBaseInfoManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntBaseInfoManageResponse);
        }
        return result;
    }

    @Override
    public Result<MchntSignInfoManageResponse> mchntSignInfoManage(MchntSignInfoManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntSignInfoManageResponse> result = new Result<>();
        MchntSignInfoManageResponse mchntSignInfoManageResponse = new MchntSignInfoManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntSignInfoManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntSignInfoManageResponse = (MchntSignInfoManageResponse) dubboResult.getResult();
            result.setResult(mchntSignInfoManageResponse);
            mchntSignInfoManageResponse.setRespCode(dubboResult.getRespCode());
            mchntSignInfoManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntSignInfoManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户签约信息管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntSignInfoManageResponse.setRespCode(((BizException) e).getCode());
                mchntSignInfoManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntSignInfoManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntSignInfoManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntSignInfoManageResponse);
        }
        return result;
    }

    @Override
    public Result<MchntSettleInfoManageResponse> mchntSettleInfoManage(MchntSettleInfoManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntSettleInfoManageResponse> result = new Result<>();
        MchntSettleInfoManageResponse mchntSettleInfoManageResponse = new MchntSettleInfoManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntSettleInfoManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntSettleInfoManageResponse = (MchntSettleInfoManageResponse) dubboResult.getResult();
            result.setResult(mchntSettleInfoManageResponse);
            mchntSettleInfoManageResponse.setRespCode(dubboResult.getRespCode());
            mchntSettleInfoManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntSettleInfoManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户结算信息管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntSettleInfoManageResponse.setRespCode(((BizException) e).getCode());
                mchntSettleInfoManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntSettleInfoManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntSettleInfoManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntSettleInfoManageResponse);
        }
        return result;
    }

    @Override
    public Result<MchntImageInfoManageResponse> mchntImageInfoManage(MchntImageInfoManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntImageInfoManageResponse> result = new Result<>();
        MchntImageInfoManageResponse mchntImageInfoManageResponse = new MchntImageInfoManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntImageInfoManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntImageInfoManageResponse = (MchntImageInfoManageResponse) dubboResult.getResult();
            result.setResult(mchntImageInfoManageResponse);
            mchntImageInfoManageResponse.setRespCode(dubboResult.getRespCode());
            mchntImageInfoManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntImageInfoManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户映像信息管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntImageInfoManageResponse.setRespCode(((BizException) e).getCode());
                mchntImageInfoManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntImageInfoManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntImageInfoManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntImageInfoManageResponse);
        }
        return result;
    }

    @Override
    public Result<MchntProdInfoManageResponse> mchntProdInfoManage(MchntProdInfoManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntProdInfoManageResponse> result = new Result<>();
        MchntProdInfoManageResponse mchntProdInfoManageResponse = new MchntProdInfoManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntProdInfoManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntProdInfoManageResponse = (MchntProdInfoManageResponse) dubboResult.getResult();
            result.setResult(mchntProdInfoManageResponse);
            mchntProdInfoManageResponse.setRespCode(dubboResult.getRespCode());
            mchntProdInfoManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntProdInfoManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户产品信息管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntProdInfoManageResponse.setRespCode(((BizException) e).getCode());
                mchntProdInfoManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntProdInfoManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntProdInfoManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntProdInfoManageResponse);
        }
        return result;
    }

    @Override
    public Result<MchntWithdrawManageResponse> mchntWithdrawManage(MchntWithdrawManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntWithdrawManageResponse> result = new Result<>();
        MchntWithdrawManageResponse mchntWithdrawManageResponse = new MchntWithdrawManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntWithdrawManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntWithdrawManageResponse = (MchntWithdrawManageResponse) dubboResult.getResult();
            result.setResult(mchntWithdrawManageResponse);
            mchntWithdrawManageResponse.setRespCode(dubboResult.getRespCode());
            mchntWithdrawManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntWithdrawManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户提现管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntWithdrawManageResponse.setRespCode(((BizException) e).getCode());
                mchntWithdrawManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntWithdrawManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntWithdrawManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntWithdrawManageResponse);
        }
        return result;
    }

    @Override
    public Result<MchntStatusManageResponse> mchntStatusManage(MchntStatusManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntStatusManageResponse> result = new Result<>();
        MchntStatusManageResponse mchntStatusManageResponse = new MchntStatusManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, MchntStatusManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            mchntStatusManageResponse = (MchntStatusManageResponse) dubboResult.getResult();
            result.setResult(mchntStatusManageResponse);
            mchntStatusManageResponse.setRespCode(dubboResult.getRespCode());
            mchntStatusManageResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(mchntStatusManageResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户状态管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntStatusManageResponse.setRespCode(((BizException) e).getCode());
                mchntStatusManageResponse.setRespDesc(e.getMessage());
            } else {
                mchntStatusManageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntStatusManageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(mchntStatusManageResponse);
        }
        return result;
    }

    @Override
    public Result<OnlineVerificationResponse> onlineVerification(OnlineVerificationRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<OnlineVerificationResponse> result = new Result<>();
        OnlineVerificationResponse onlineVerificationResponse = new OnlineVerificationResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, OnlineVerificationResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            onlineVerificationResponse = (OnlineVerificationResponse) dubboResult.getResult();
            result.setResult(onlineVerificationResponse);
            onlineVerificationResponse.setRespCode(dubboResult.getRespCode());
            onlineVerificationResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(onlineVerificationResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("联网核查处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                onlineVerificationResponse.setRespCode(((BizException) e).getCode());
                onlineVerificationResponse.setRespDesc(e.getMessage());
            } else {
                onlineVerificationResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                onlineVerificationResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(onlineVerificationResponse);
        }
        return result;
    }

    @Override
    public Result<CreditReportQueryResponse> creditReportQuery(CreditReportQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CreditReportQueryResponse> result = new Result<>();
        CreditReportQueryResponse creditReportQueryResponse = new CreditReportQueryResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, CreditReportQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            creditReportQueryResponse = (CreditReportQueryResponse) dubboResult.getResult();
            result.setResult(creditReportQueryResponse);
            creditReportQueryResponse.setRespCode(dubboResult.getRespCode());
            creditReportQueryResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(creditReportQueryResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("征信查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditReportQueryResponse.setRespCode(((BizException) e).getCode());
                creditReportQueryResponse.setRespDesc(e.getMessage());
            } else {
                creditReportQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditReportQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(creditReportQueryResponse);
        }
        return result;
    }

    @Override
    public Result<AcctRealNameAuthResponse> acctRealNameAuth(AcctRealNameAuthRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AcctRealNameAuthResponse> result = new Result<>();
        AcctRealNameAuthResponse acctRealNameAuthResponse = new AcctRealNameAuthResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, AcctRealNameAuthResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            acctRealNameAuthResponse = (AcctRealNameAuthResponse) dubboResult.getResult();
            result.setResult(acctRealNameAuthResponse);
            acctRealNameAuthResponse.setRespCode(dubboResult.getRespCode());
            acctRealNameAuthResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(acctRealNameAuthResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("账户实名认证处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                acctRealNameAuthResponse.setRespCode(((BizException) e).getCode());
                acctRealNameAuthResponse.setRespDesc(e.getMessage());
            } else {
                acctRealNameAuthResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                acctRealNameAuthResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(acctRealNameAuthResponse);
        }
        return result;
    }

    @Override
    public Result<OcrResponse> ocrService(OcrRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<OcrResponse> result = new Result<>();
        OcrResponse ocrResponse = new OcrResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, OcrResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            ocrResponse = (OcrResponse) dubboResult.getResult();
            result.setResult(ocrResponse);
            ocrResponse.setRespCode(dubboResult.getRespCode());
            ocrResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(ocrResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("OCR识别处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                ocrResponse.setRespCode(((BizException) e).getCode());
                ocrResponse.setRespDesc(e.getMessage());
            } else {
                ocrResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                ocrResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(ocrResponse);
        }
        return result;
    }

    @Override
    public Result<SettleAccountSignResponse> settleAccountSign(SettleAccountSignRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<SettleAccountSignResponse> result = new Result<>();
        SettleAccountSignResponse settleAccountSignResponse = new SettleAccountSignResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, SettleAccountSignResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            settleAccountSignResponse = (SettleAccountSignResponse) dubboResult.getResult();
            result.setResult(settleAccountSignResponse);
            settleAccountSignResponse.setRespCode(dubboResult.getRespCode());
            settleAccountSignResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(settleAccountSignResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("结算账户核心签约处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                settleAccountSignResponse.setRespCode(((BizException) e).getCode());
                settleAccountSignResponse.setRespDesc(e.getMessage());
            } else {
                settleAccountSignResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                settleAccountSignResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(settleAccountSignResponse);
        }
        return result;
    }

    @Override
    public Result<ImageFileUpDownloadResponse> imageFileUpDownload(ImageFileUpDownloadRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ImageFileUpDownloadResponse> result = new Result<>();
        ImageFileUpDownloadResponse imageFileUpDownloadResponse = new ImageFileUpDownloadResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, ImageFileUpDownloadResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            imageFileUpDownloadResponse = (ImageFileUpDownloadResponse) dubboResult.getResult();
            result.setResult(imageFileUpDownloadResponse);
            imageFileUpDownloadResponse.setRespCode(dubboResult.getRespCode());
            imageFileUpDownloadResponse.setRespDesc(dubboResult.getRespDesc());
            result.setResult(imageFileUpDownloadResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("图片或文件上传下载处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                imageFileUpDownloadResponse.setRespCode(((BizException) e).getCode());
                imageFileUpDownloadResponse.setRespDesc(e.getMessage());
            } else {
                imageFileUpDownloadResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                imageFileUpDownloadResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(imageFileUpDownloadResponse);
        }
        return result;
    }
}
